package com.dys.service.impl;

import com.dys.mapper.EmpMapper;
import com.dys.pojo.Emp;
import com.dys.pojo.EmpExample;
import com.dys.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: com.dys.service.impl.EmpServiceImpl
 * @description: com.dys.service.impl
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/11/11 22:50
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public Emp queryEmpById(int id) {
        return empMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Emp> query(Emp emp) {
        EmpExample empExample = new EmpExample();
        // 绑定查询的条件
        EmpExample.Criteria criteria = empExample.createCriteria();
        if (!"".equals(emp.getName()) && emp.getName() != null) {
            criteria.andNameLike("%" + emp.getName() + "%");
        }

        if (emp.getId() != null && emp.getId() > 0) {
            criteria.andIdEqualTo(emp.getId());
        }
        if (emp.getAge() != null && emp.getAge() > 0) {
            criteria.andAgeEqualTo(emp.getAge());
        }
        return empMapper.selectByExample(empExample);
    }

    @Override
    public Integer insert(Emp emp) {
        return empMapper.insertSelective(emp);
    }

    @Override
    public Integer updateById(Emp emp) {
        return empMapper.updateByPrimaryKeySelective(emp);
    }

    @Override
    public Integer deleteById(Emp emp) {
        return empMapper.deleteByPrimaryKey(emp.getId());
    }
}
